package cheme.ProcessModel.Table;

import java.io.Serializable;

import org.eclipse.draw2d.geometry.Rectangle;

import cheme.ProcessModel.AbstractChild;
import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.IXmlOperations;
import cheme.ProcessModel.Visitors.IModelVisitor;

/*
 * The basic core of the text editing GUI of ChemE, this type does not emit
 * events as that is left up to specialized subtypes. As such this is a basic
 * container of text and bounds information to control the appearance of field
 * within the presentation layer.
 */

public class Field extends AbstractChild implements IField, IChild, Serializable, IXmlOperations
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;
	
	public transient static final String DEFAULT_TEXT = "";
	
	/*
	 * The text that will be dispalyed (and edited if applicable) in the
	 * presentation layer.
	 */
	
	protected String text = DEFAULT_TEXT;
	
	/*
	 * The text that will be diaplyed upon mouseover of this elements
	 * representation in the presentation layer.
	 */
	
	private String toolTipText = "";
	
	/*
	 * The locaton and size of this model element's representation in the
	 * presentation layer. This only applies to fields placed within a XYLayout
	 * organized aprent; In many cases this of overriden by the field's
	 * inclusion in a constrained layout parent which fits this guy into a grid
	 * or some other structure.
	 */
	
	private Rectangle bounds = new Rectangle(0,0,15,25);
	
	public Field(IModelElement parent, String text)
	{
		super(parent);
		
		this.text = text;
	}
	
	/**
	 * @return the text
	 */
	public String getText()
	{
		return text;
	}

	/**
	 * @return the bounds
	 */
	public Rectangle getBounds()
	{
		return bounds.getCopy();
	}

	/**
	 * @param bounds the bounds to set
	 */
	public void setBounds(Rectangle bounds)
	{
		if(!this.bounds.equals(bounds))
		{
			this.bounds = bounds;
			this.firePropertyChange(P_CONSTRAINT, null, bounds);
		}
	}

	/**
	 * @return the toolTipText
	 */
	public String getToolTipText()
	{
		return toolTipText;
	}

	/**
	 * @param toolTipText the toolTipText to set
	 */
	public void setToolTipText(String toolTipText)
	{
		this.toolTipText = toolTipText;
	}
	
	public void set(Field value)
	{
		text = value.getText();
	}
	
	@Override
	public String toString()
	{
		return getText();
	}
	
	public Object accept(IModelVisitor visitor)
	{
		visitor.visit(this);
		
		return IModelVisitor.NullReturn;
	}
	
	public Object fromXml() {
		// TODO Auto-generated method stub
		return null;
	}

	public String toXml() {
		String xml = "";
		xml += "<Field type=\"generic\">";
		xml += "<Text>"+ this.text +"</Text>";
		xml += "</Field>";
		return xml;
	}
	
}
